package com.solo.servlet;

import com.solo.entity.User;
import com.solo.service.UserService;
import com.solo.service.impl.UserServiceImpl;
import com.solo.utils.DBUtil;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "UserServlet", value = "/UserServlet")
public class UserServlet extends HttpServlet {
    private UserService userService;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 汉字编码设置
        // request.setCharacterEncoding("utf-8");
        // ArrayList<User> userList = new ArrayList<User>();
        // userList.add(new User("admin", "264309"));
        // userList.add(new User("user1", "123456"));
        // userList.add(new User("user2", "123"));
        // request.setAttribute("userList", userList);
        // request.getRequestDispatcher("/user.jsp").forward(request, response);

        // String action = request.getServletPath();
        //获取对应的请求参数
        String action = request.getParameter("method");

        System.out.println("action===" + action);

        try {
            switch (action) {
                case "new":
                    showNewForm(request, response);
                    break;
                case "add":
                    addUser(request, response);
                    break;
                case "/delete":
                    deleteUser(request, response);
                    break;
                case "/edit":
                    showEditForm(request, response);
                    break;
                case "/update":
                    updateUser(request, response);
                    break;
                default:
                    listUser(request, response);
                    break;
            }
        } catch (Exception ex) {
            throw new ServletException(ex);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // // 汉字编码设置
        // request.setCharacterEncoding("utf-8");
        //
        // ArrayList<User> userList = new ArrayList<User>();
        //
        // userList.add(new User("admin", "264309"));
        // userList.add(new User("user1", "123456"));
        // userList.add(new User("user2", "123"));
        //
        // request.setAttribute("userList", userList);
        //
        // request.getRequestDispatcher("/user.jsp").forward(request, response);
        doGet(request, response);
    }

    private void showNewForm(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher dispatcher = request.getRequestDispatcher("views/addUser.jsp");
        dispatcher.forward(request, response);
    }


    private void addUser(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        User user = new User(username, password);
        userService = new UserServiceImpl();
        userService.addUser(user);
        // response.sendRedirect("user.jsp");
        listUser(request, response);
    }

    private void deleteUser(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Long id = Long.parseLong(request.getParameter("id"));

        User user = new User(id);
        userService = new UserServiceImpl();
        userService.deleteUser(user);
        response.sendRedirect("list");
    }

    private void showEditForm(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Long id = Long.parseLong(request.getParameter("id"));

        userService = new UserServiceImpl();
        User user = userService.selectUserById(id);
        RequestDispatcher dispatcher = request.getRequestDispatcher("views/addUser.jsp");
        request.setAttribute("user", user);
        dispatcher.forward(request, response);
    }
    private void updateUser(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        Long id = Long.parseLong(request.getParameter("id"));
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        User user = new User(id, username, password);

        userService = new UserServiceImpl();
        userService.updateUser(user);
        response.sendRedirect("list");
    }

    private void listUser(HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        userService = new UserServiceImpl();
        List<User> listUser = userService.selectAllUser();
        request.setAttribute("userList", listUser);
        RequestDispatcher dispatcher = request.getRequestDispatcher("views/user.jsp");
        dispatcher.forward(request, response);
    }
}